home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 031a / rsgage.zip / RESGAUGE.C < prev    next >
C/C++ Source or Header  |  1991-06-04  |  8KB  |  283 lines

  1. //    =========================================================================
  2. //    resgauge.c -- a gauge of free system resources.
  3. //    =========================================================================
  4.  
  5.  
  6. #include    <windows.h>
  7. #include    "resgauge.h"
  8.  
  9.  
  10. //    -----------------
  11. //    manifest constant
  12. //    -----------------
  13. #define    TIMER_ID    1
  14.  
  15. //    -------------------
  16. //    function prototypes
  17. //    -------------------
  18. LONG    FAR        PASCAL    WndProc(HWND, WORD, WORD, LONG);
  19. BOOL    FAR        PASCAL    About(HWND, WORD, WORD, LONG);
  20. WORD    NEAR    PASCAL    GetSystemResources(void);
  21.  
  22. //    an undocumented Windows function ... one of many!
  23. DWORD    FAR        PASCAL    GetHeapSpaces(HANDLE);
  24.  
  25. //    ------
  26. //    global
  27. //    ------
  28. HANDLE    hInst;
  29.  
  30.  
  31. //    =========================================================================
  32. //    > > > > > > > > > > > > > > >  code begins  < < < < < < < < < < < < < < <
  33. //    =========================================================================
  34. //    The "main()" for Windows: calls the initialization functions and enters
  35. //    the message processing loop.  Returns the wParam element of the MSG when
  36. //    a WM_QUIT message has been processed by the window function.
  37. //    -------------------------------------------------------------------------
  38. short
  39. PASCAL
  40. WinMain(
  41. HANDLE                hInstance,        // current instance
  42. HANDLE                hPrevInstance,    // previous instance
  43. LPSTR                lpszCmdLine,    // command line
  44. short                nCmdShow)        // window state (usually open or iconic)
  45. {
  46.     static    char    szAppName[]   = "Resource Gauge v1.0";
  47.     static    char    szClassName[] = "ResourceGauge";
  48.  
  49.     HWND            hWnd;
  50.     MSG                msg;
  51.     WNDCLASS        wndclass;
  52.  
  53.     if (!hPrevInstance)
  54.     {
  55.         wndclass.style         = CS_HREDRAW | CS_VREDRAW;
  56.         wndclass.lpfnWndProc   = WndProc;
  57.         wndclass.cbClsExtra    = 0;
  58.         wndclass.cbWndExtra    = 0;
  59.         wndclass.hInstance     = hInstance;
  60.         wndclass.hIcon         = NULL;
  61.         wndclass.hCursor       = LoadCursor(NULL, IDC_ARROW);
  62.         wndclass.hbrBackground = NULL;
  63.         wndclass.lpszMenuName  = szClassName;
  64.         wndclass.lpszClassName = szClassName;
  65.         RegisterClass(&wndclass);
  66.     }
  67.     else
  68.     {
  69.         return(FALSE);
  70.     }
  71.     hInst = hInstance;
  72.     hWnd = CreateWindow(szClassName, szAppName, WS_OVERLAPPEDWINDOW,
  73.       CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
  74.       NULL, NULL, hInstance, NULL);
  75.     if (!SetTimer(hWnd, TIMER_ID, 1000, NULL))
  76.     {
  77.         MessageBox(hWnd, "Too many timers in use!", szAppName,
  78.             MB_ICONEXCLAMATION | MB_OK);
  79.         return(FALSE);
  80.     }
  81.     ShowWindow(hWnd, SW_SHOWMINNOACTIVE);
  82.     UpdateWindow(hWnd);
  83.     while (GetMessage(&msg, NULL, 0, 0))
  84.     {
  85.         TranslateMessage(&msg);
  86.         DispatchMessage(&msg);
  87.     }
  88.     return(msg.wParam);
  89. } // WinMain
  90.  
  91.  
  92. //    -------------------------------------------------------------------------
  93. //    Processes messages for the main window.
  94. //    -------------------------------------------------------------------------
  95. LONG
  96. FAR
  97. PASCAL
  98. WndProc(
  99. HWND                hWnd,        // window handle
  100. WORD                wMessage,    // type of message
  101. WORD                wParam,        // 16 bits of information
  102. LONG                lParam)        // 32 additional bits of information
  103. {
  104.     static    WORD    wFree;
  105.  
  106.     char            szBuffer[4];
  107.     short            x;
  108.     short            xText;
  109.     short            yText;
  110.     DWORD            dwExtent;
  111.     HDC                hDC;
  112.     HMENU            hMenu;
  113.     LONG            lReturn;
  114.     PAINTSTRUCT        ps;
  115.     RECT            rect;
  116.     RECT            rectTmp;
  117.     WORD            wTmp;
  118.  
  119.     lReturn = 0L;
  120.     switch (wMessage)
  121.     {
  122.         case WM_CREATE:
  123.             hMenu = GetSystemMenu(hWnd, FALSE);
  124.             ChangeMenu(hMenu, 0, NULL, 0, MF_APPEND);
  125.             ChangeMenu(hMenu, 0, "A&bout...", IDM_ABOUT, MF_APPEND);
  126.             ChangeMenu(hMenu, SC_MINIMIZE, "Mi&nimize", SC_CLOSE,
  127.               MF_CHANGE | MF_DISABLED | MF_GRAYED);
  128.             ChangeMenu(hMenu, SC_MAXIMIZE, "Ma&ximize", SC_CLOSE,
  129.               MF_CHANGE | MF_DISABLED | MF_GRAYED);
  130.             ChangeMenu(hMenu, SC_RESTORE, "&Restore", SC_CLOSE,
  131.               MF_CHANGE | MF_DISABLED | MF_GRAYED);
  132.             wFree = GetSystemResources();
  133.             break;
  134.         case WM_TIMER:
  135.             wTmp = GetSystemResources();
  136.             if (wTmp != wFree)
  137.             {
  138.                 wFree = wTmp;
  139.                 InvalidateRect(hWnd, NULL, TRUE);
  140.             }
  141.             break;
  142.         case WM_SYSCOMMAND:
  143.             if (wParam == IDM_ABOUT)
  144.                 DialogBox(hInst, "About", hWnd, About);
  145.             else
  146.                 lReturn = DefWindowProc(hWnd, wMessage, wParam, lParam);
  147.             break;
  148.         case WM_PAINT:
  149.             // The "gas gauge" is created by drawing a text string stating
  150.             // the percentage into the middle of the gas gauge rectangle and
  151.             // by separating that rectangle into two parts: rectTmp (the
  152.             // left part, filled in blue) and rectTmp (the right part,
  153.             // filled in white).  x is the x coordinate that divides these
  154.             // two rectangles.  The text in the blue rectangle is drawn
  155.             // white and vice versa.  This is easy to do with ExtTextOut()!
  156.             hDC = BeginPaint(hWnd, &ps);
  157.             wsprintf(szBuffer, "%d%%", wFree);
  158.             GetClientRect(hWnd, &rect);
  159.             SelectObject(hDC, GetStockObject(ANSI_VAR_FONT));
  160.             dwExtent = GetTextExtent(hDC, szBuffer, lstrlen(szBuffer));
  161.             x = ((rect.right - rect.left) * (100 - wFree)) / 100;
  162.             xText = rect.left +
  163.               ((rect.right - rect.left) - LOWORD(dwExtent)) / 2;
  164.             yText = rect.top +
  165.               ((rect.bottom - rect.top) - HIWORD(dwExtent)) / 2;
  166.             // paint in the "used" rectangle of the gas gauge with blue
  167.             // background and white text
  168.             SetRect(&rectTmp, rect.left, rect.top, rect.left + x,
  169.               rect.bottom);
  170.             SetTextColor(hDC, RGB(255, 255, 255));
  171.             SetBkColor(hDC, RGB(0, 0, 255));
  172.             ExtTextOut(hDC, xText, yText, ETO_CLIPPED | ETO_OPAQUE,
  173.               &rectTmp, szBuffer, lstrlen(szBuffer), NULL);
  174.             // paint in the "free" rectangle of the gas gauge with white
  175.             // background and blue text
  176.             SetRect(&rectTmp, rect.left + x, rect.top,
  177.               rect.right, rect.bottom);
  178.             SetTextColor(hDC, RGB(0, 0, 255));
  179.             SetBkColor(hDC, RGB(255, 255, 255));
  180.             ExtTextOut(hDC, xText, yText, ETO_CLIPPED | ETO_OPAQUE,
  181.               &rectTmp, szBuffer, lstrlen(szBuffer), NULL);
  182.             EndPaint(hWnd, &ps);
  183.             break;
  184.         case WM_QUERYOPEN:
  185.             break;
  186.         case WM_DESTROY:
  187.             KillTimer(hWnd, TIMER_ID);
  188.             PostQuitMessage(0);
  189.             break;
  190.         default:
  191.             lReturn = DefWindowProc(hWnd, wMessage, wParam, lParam);
  192.             break;
  193.     }
  194.     return(lReturn);
  195. } // WndProc
  196.  
  197.  
  198. //    -------------------------------------------------------------------------
  199. //    Processes messages for the "About ..." dialog box.
  200. //    -------------------------------------------------------------------------
  201. BOOL
  202. FAR
  203. PASCAL
  204. About(
  205. HWND        hDlg,        // window handle of the dialog
  206. WORD        wMessage,    // type of message
  207. WORD        wParam,        // 16 bits of information
  208. LONG        lParam)        // 32 additional bits of information
  209. {
  210.     short    x;
  211.     short    y;
  212.     short    xSize;
  213.     short    ySize;
  214.     BOOL    bReturn;
  215.     RECT    rect;
  216.  
  217.     bReturn = FALSE;
  218.     switch (wMessage)
  219.     {
  220.         case WM_INITDIALOG:
  221.             // center the dialog
  222.             GetWindowRect(hDlg, &rect);
  223.             xSize = rect.right - rect.left;
  224.             ySize = rect.bottom - rect.top;
  225.             x = GetSystemMetrics(SM_CXSCREEN);
  226.             y = GetSystemMetrics(SM_CYSCREEN);
  227.             MoveWindow(hDlg, (x - xSize) / 2, (y - ySize) / 2,
  228.               xSize, ySize, TRUE);
  229.             // set the font of the text boxes
  230.             SendMessage(GetDlgItem(hDlg, IDOK), WM_SETFONT,
  231.               GetStockObject(ANSI_VAR_FONT), FALSE);
  232.             for (x = IDD_ABOUT1; x <= LAST_ABOUT; ++x)
  233.             {
  234.                 SendMessage(GetDlgItem(hDlg, x), WM_SETFONT,
  235.                   GetStockObject(ANSI_VAR_FONT), FALSE);
  236.             }
  237.             bReturn = TRUE;
  238.             break;
  239.         case WM_COMMAND:
  240.             if ((wParam == IDOK) || (wParam == IDCANCEL))
  241.             {
  242.                 EndDialog(hDlg, TRUE);
  243.                 bReturn = TRUE;
  244.             }
  245.             break;
  246.     }
  247.     return(bReturn);
  248. } // About
  249.  
  250.  
  251. //    -------------------------------------------------------------------------
  252. //    Uses an undocumented system call and the algorithm given in TIPS.TXT to
  253. //    calculate the percentage of free system resources.
  254. //    -------------------------------------------------------------------------
  255. WORD
  256. NEAR
  257. PASCAL
  258. GetSystemResources(void)
  259. {
  260.     WORD    wFree;
  261.     WORD    wGDI;
  262.     WORD    wSize;
  263.     WORD    wUser;
  264.     DWORD    dwInfo;
  265.  
  266.     // get the information on the GDI module
  267.     dwInfo = GetHeapSpaces(GetModuleHandle("GDI"));
  268.     wSize  = HIWORD(dwInfo);
  269.     wFree  = LOWORD(dwInfo);
  270.     wGDI   = LOWORD(((DWORD) wFree) * 100 / wSize);
  271.     // get the information on the User module
  272.     dwInfo = GetHeapSpaces(GetModuleHandle("User"));
  273.     wSize  = HIWORD(dwInfo);
  274.     wFree  = LOWORD(dwInfo);
  275.     wUser  = LOWORD(((DWORD) wFree) * 100 / wSize);
  276.     return(min(wGDI, wUser));
  277. } // GetSystemResources
  278.  
  279.  
  280. //    =================
  281. //    end of resgauge.c
  282. //    =================
  283.